﻿using System.Collections.Generic;
using System.Text.RegularExpressions;
using AutoBuildEntity.Model;

namespace AutoBuildEntity.Common
{
    public static class TableColumnHelper
    {
        public static string ToCSharpType(string type)
        {
            type = type.ToLower();
            type= Regex.Replace(type, @"\(\d+\)", "");
            switch (type)
            {
                case "int":
                {
                   return "int";
                }
                case "nvarchar":
                {
                    return  "string";
                }
                case "decimal":
                {
                    return "decimal";
                }
                case "numeric":
                {
                    return "long";
                }
                case "bigint":
                {
                    return "long";
                }
                case "tinyint":
                {
                    return "sbyte";
                }
                case "tinyint unsigned"://tinyint(3) unsigned
                {
                    return "byte";
                }
                case "numeric+":
                {
                    return "decimal";
                }
                case "datetime":
                {
                    return "DateTime";
                }
                case "image":
                {
                    return "byte[]";
                }
                case "nchar":
                {
                    return  "string";
                }
                case "varchar":
                {
                    return "string";
                }
                case "char":
                {
                    return "string";
                }
                case "bit":
                {
                    return "bool";
                }
                case "uniqueidentifier":
                {
                    return "Guid";
                }
                case "text":
                {
                    return "string";
                }
                case "smallint":
                {
                    return "short";
                }
                case "smallint unsigned":
                {
                    return "ushort";
                }
                default:
                {
                    return type;
                }
            }
        }

        /// <summary>
        ///     首字母转大写
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string ToUpperFirst(string str)
        {
            return Regex.Replace(str, @"\b[a-z]\w+", delegate(Match match)
            {
                var v = match.ToString();
                return char.ToUpper(v[0]) + v.Substring(1);
            });
        }

        /// <summary>
        ///     数据的列的信息转换成模板所需要的信息
        /// </summary>
        /// <param name="input"></param>
        public static void ConvertToCs(List<TableColumnModel> input)
        {
           
            foreach (var item in input)
            {
                item.CSharpName = ToUpperFirst(UpperEveryWord(item.Name));
                item.JsonNameWithUpper = item.CSharpName;
                item.JsonNameWith_ = SplitWord(item.Name);
                var dataType = ToCSharpType(item.DataType);
                if (item.IsNull == "YES" && dataType != "string")
                    item.CSharpType = dataType + "?";
                else
                    item.CSharpType = dataType;
            }
        }

        /// <summary>
        ///     -分割的每个单词首字母都大写
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public static string UpperEveryWord(string input)
        {
            return ToUpperFirst(Regex.Replace(input, @"_([a-z])", delegate(Match match)
            {
                var v = match.ToString();
                return char.ToUpper(v[1]).ToString();
            }));
        }

        /// <summary>
        ///     驼峰使用下划线分隔开
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public static string SplitWord(string input)
        {
            return Regex.Replace(input, @"[A-Z]", delegate(Match match)
            {
                var v = match.ToString();
                return "_" + char.ToLower(v[0]) + v.Substring(1);
            }).Trim('_');
        }
    }
}